//
//  ESTFollowUpHistoryInfoCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/19.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTFollowUpHistoryInfoCell.h"
#import "ESTFollowUpHistoryModel.h"
#import "UILabel+Space.h"

@interface ESTFollowUpHistoryInfoCell ()
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeIntegerLabel;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeight;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
/**
 *  总共几个记录
 */
@property(nonatomic, assign) NSInteger modelNumber;
@end

@implementation ESTFollowUpHistoryInfoCell

+ (instancetype)creatFollowUpHistoryInfoCell:(UITableView *)tableView {
    return [self creatFollowUpHistoryInfoCell:tableView modelNumber:-1];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.bottomView.backgroundColor = ESTBackgroundColor;
    [self.dateLabel changeLineSpaceSpace:10];
}
/**
 历史跟进的Cell

 @param tableView tableView
 @param modelNumber 总共几个
 @return 创建Cell
 */
+ (instancetype)creatFollowUpHistoryInfoCell:(UITableView *)tableView modelNumber:(NSInteger)modelNumber {
    ESTFollowUpHistoryInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTFollowUpHistoryInfoCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ESTFollowUpHistoryInfoCell" owner:nil options:nil].lastObject;
    }
    cell.modelNumber = modelNumber;
    return cell;
}
- (void)setModel:(ESTFollowUpHistoryModel *)model {
    _model = model;
    NSString *dateString = [model.FOLLOWUP_TIME stringByReplacingOccurrencesOfString:@"+" withString:@"\n"];
    self.dateLabel.text = dateString;
    self.timeIntegerLabel.text = model.LINK_TIME;
    [self setupMainView];
}
- (void)setupMainView {

    if (self.modelNumber > 0) {
        self.topViewHeight.constant = 20;
        TYZLabel *label = [TYZLabel labelWithText:[NSString stringWithFormat:@"%@%ld%@", [@"回访记录" S], (long)self.modelNumber, [@"次" S]]];
        [self.topView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.dateLabel);
            make.bottom.equalTo(self.topView).offset(-3);
        }];
        label.font = self.dateLabel.font;
        label.textColor = self.dateLabel.textColor;
        label.textAlignment = self.dateLabel.textAlignment;
    } else {
        self.topViewHeight.constant = 10;
    }
    
    NSArray<NSString *> *arr1 = [self.model.FOLLOWUP_LABLE componentsSeparatedByString:@","];
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr1];
    [arr1 enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.length == 0) {
            [arrM removeObject:obj];
        }
    }];
    arr1 = [arrM mutableCopy];
    NSInteger lines = 0;
    CGFloat height = 0;
    if (arr1.count > 0) {
        lines = arr1.count / 4 + 1;
        if (arr1.count % 4 == 0) {
            lines--;
        }
        height = FollowUpHistoryInfoCell_LabelLineHeight;
    }

    UIFont *font = TYZFont(CoreInfo.fontSize * 0.6);
    for (NSInteger i = 0; i < lines; i++) {
        CGFloat leftMargin = 2;
        for (NSInteger j = 0; j < 4; j++) {
            if (i * 4 + j >= arr1.count) {
                break;
            }
            NSString *title = arr1[i * 4 + j];
            TYZLabel *label = [TYZLabel labelWithText:title];
            CGSize size = [title getSizeWithFont:font];
            [self.mainView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mainView).offset(height * i);
                make.left.equalTo(self.mainView).offset(leftMargin);
                make.width.mas_equalTo(size.width + 15);
                make.height.mas_equalTo(size.height + 10);
            }];
            label.font = font;
            label.textColor = TYZWhiteColor;
            label.layer.masksToBounds = YES;
            label.backgroundColor = ThemeColor;
            label.layer.cornerRadius = (size.height + 10) * 0.5;
            label.textAlignment = NSTextAlignmentCenter;
            leftMargin += size.width + 15 + FollowUpHistoryInfoCell_LineSpace;
        }
    }
    height *= lines;
    
    NSString *text = [self.model.REMARK stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
    if ([text hasSuffix:@"\n"]) {
        text = [text substringToIndex:text.length - 1];
    }
    TYZLabel *remarkLabel = [[TYZLabel alloc] init];
    remarkLabel.text = text;
    remarkLabel.numberOfLines = 0;
    [remarkLabel changeLineSpaceSpace:5];
    remarkLabel.textColor = TYZBlackColor;
    [self.mainView addSubview:remarkLabel];
    remarkLabel.font = TYZFont(CoreInfo.fontSize * 0.8);
    [remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainView).offset(height);
        make.left.equalTo(self.mainView).offset(2);
        make.right.equalTo(self.mainView);
        make.bottom.equalTo(self.mainView).offset(-FollowUpHistoryInfoCell_LinePadding * 0.5);
    }];
}
@end
